/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

Rectangle {
    id: carpoolselectiondlg
    width: 300
    height: 230
    gradient: Gradient {
        GradientStop {
            position: 0.44
            color: "#abd1ee"
        }

        GradientStop {
            position: 1
            color: "#000000"
        }
    }
    z: 50



    signal join(string carpoolkarpoid)
    signal cancel()

    property variant carpoolinfo: []      // [ [carpoolkarpoid,name,description,ownerkarpoid], ... ]
    property variant carpoolownerinfo: [] // [ [ownerkarpoid,ownerusername,ownerphone], ... ]


    function setCarpoolInfo(carpoolinfoarr,carpoolownerinfoarr)
    {
        carpoolselectiondlg.carpoolinfo = carpoolinfoarr
        carpoolselectiondlg.carpoolownerinfo = carpoolownerinfoarr

        var cparray = []
        for (var r = 0; r < carpoolselectiondlg.carpoolinfo.length; r++)
        {
            cparray[r] = carpoolselectiondlg.carpoolinfo[r][1]
        }

        carpoolcombobox.setComboItems(cparray)

        if (carpoolselectiondlg.carpoolinfo.length > 0)
        {
            carpoolcombobox.setSelectedItem(cparray[0])
            carpoolcombobox.selectedind = 0

            infoid.text = "Owner: " + getUsername(carpoolselectiondlg.carpoolinfo[0][3])
            infoid2.text = "Description: " + carpoolselectiondlg.carpoolinfo[0][2]

        }


    }

    function getUsername(ownerkarpoid)
    {
        for (var i = 0; i < carpoolownerinfo.length; i++)
        {
            if (carpoolownerinfo[i][0] == ownerkarpoid)
            {
                return  carpoolownerinfo[i][1]
            }
        }

        return "NA"
    }

    function getUserphone(ownerkarpoid)
    {
        for (var i = 0; i < carpoolownerinfo.length; i++)
        {
            if (carpoolownerinfo[i][0] == ownerkarpoid)
            {
                return  carpoolownerinfo[i][2]
            }
        }

        return "NA"
    }
    Rectangle
    {
        id: titlerect
        width: 110
        height: 29
        color: "#5d8eac"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        Text {
            id: titleid
            width: 110
            height: 29
            text: "Carpool selection"
            font.family: "Arial"
            anchors.leftMargin: 10
            anchors.fill: parent
            anchors.verticalCenter: titlerect.verticalCenter
            font.pixelSize: 16
        }

        MouseArea {
            id: titlerectmousearea
            anchors.fill: parent

            drag.target: carpoolselectiondlg
            drag.axis: Drag.XandYAxis
            drag.minimumX: -1000
            drag.maximumX: 1000
            drag.minimumY: -1000
            drag.maximumY: 1000
        }
    }

    property int labelwidth: 90
    property int labelfontpixesize: 12
    property int inputboxheight: 26

    property string routename: ""

    Text {
        id: routeid
        text: "Select carpool for route: " + routename
        anchors.top:  titlerect.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        font.pixelSize: labelfontpixesize
    }

    ComboBox {
        id: carpoolcombobox
        deletebutton: false
        height: carpoolselectiondlg.inputboxheight
        anchors.top:  routeid.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10


        onItemSelected:
        {
            var ind = carpoolcombobox.selectedind

            infoid.text = "Owner: " + getUsername(carpoolselectiondlg.carpoolinfo[ind][3])
            infoid2.text = "Description: " + carpoolselectiondlg.carpoolinfo[ind][2]
        }


    }

    Text {
        id: infoid
        text: ""
        anchors.top:  carpoolcombobox.bottom
        anchors.topMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        font.pixelSize: labelfontpixesize
    }

    Text {
        id: infoid2
        text: ""
        anchors.top:  infoid.bottom
        anchors.topMargin: 5
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        font.pixelSize: labelfontpixesize
    }

    PushButton
    {
        id: joinid
        label: "Join"
        width: 80
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10


        onButtonpressed:
        {
            if (carpoolcombobox.selectedind < 0)
            {
                messagebox.text = "No carpool selected."
                messagebox.show()
            }
            else
            {
                var ind = carpoolcombobox.selectedind
                var carpoolkarpoid = carpoolinfo[ind][0]
                carpoolselectiondlg.join(carpoolkarpoid)
            }
        }

    }

    PushButton
    {
        id: cancelid
        label: "Cancel"
        width: 80
        anchors.right: joinid.left
        anchors.rightMargin: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10

        onButtonpressed:
        {
           carpoolselectiondlg.visible = false
           carpoolselectiondlg.cancel()
        }

    }
}
